import Vue from 'vue';
import VueRouter from 'vue-router';
import { get } from '@/common/utils';

Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "Login" */ '@/views/login/Login.vue'),
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import(/* webpackChunkName: "Admin" */ '@/views/admin/Admin.vue'),
    children: [
      {
        path: 'restaurant',
        name: 'Restaurant',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "Restaurant" */ '@/views/admin/components/restaurant/Restaurant.vue'),
      },
      {
        path: 'menu',
        name: 'Menu',
        component: () => import(/* webpackChunkName: "Menu" */ '@/views/admin/components/menu/Menu.vue'),
      },
      {
        path: 'order',
        name: 'Order',
        component: () => import(/* webpackChunkName: "Auth" */ '@/views/admin/components/Order/Order.vue'),
        beforeEnter: (to, from, next) => {
          if (get('loginName') === 'admin') {
            next();
          } else if (get('loginName') === 'employee') {
            next();
          } else {
            next({
              path: '/admin/restaurant',
            });
          }
        },
      },
    ],
  },

  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '*',
    component: () => import(/* webpackChunkName: "404" */ '@/views/404page/404Page.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

export default router;
